<script type="text/javascript">
	$(document).ready(function(){
		$('#petition').dataTable();
	});

	function paidCheque(cheque_no){
		var task = "paid";
			$.post('process_status_utility.php',{task: task, cheque_no: cheque_no}, function(data) {
					window.location.href="index.php?pages=paid_utility";
			});
	}

	function approvedCheque(cheque_no){
		var task = "approvedCheque";
		
		$('<div>').dialog2({
			autoOpen: true,
			title: "อนุมัติ",
			content: "dlg_date_approved_utility.php?cheque_no=" + cheque_no + "&task=" + task,
			id: "modal-dlgDate"
		});
	}

	function disapprovalCheque(cheque_no){
		var task = "disapprovalCheque";

		$.post('process_status_utility.php',{task: task, cheque_no: cheque_no}, function(data) {
			window.location.href="index.php?pages=paid_utility";
		});

	}

</script>

<?php
	
	//require_once("includes/connect.php");

	$db = new myDBS;
	$db->selectDB("account");

	$sql = "SELECT  cheque_no ,  bank_name ,  branch_name, cheque_status, paid_status, sum(utility) AS sum
		FROM  tbl_utility 
		WHERE  (cheque_no IS NOT NULL and cheque_no <> '')
		AND approved_status =  'อนุมัติ'
		GROUP BY  cheque_no ,  bank_name ,  branch_name, cheque_status, paid_status 
		ORDER BY  cheque_no
		";
	$result = $db->runSQL($sql);


?>
	<table id="petition" class="table table-striped" width="100%" >
		<caption><br></caption>
		<thead>
			<tr>
				<th>#</th>
				<th>เลขที่เช็ค</th>
				<th>ชื่อธนาคาร</th>
				<th>สาขา</th>
				<th>จำนวนเงิน</th>
				<th>อนุมัติเช็ค</th>
				<th>จ่ายเช็ค</th>
			</tr>
		</thead>
		<tbody>
			<?php
				$i=0;
				while ( $rows = $db->fetch_object ( $result )){
					$i++;
			?>
			<tr>
				<td><?php echo $i; ?></td>
				<td><?php echo $rows->cheque_no; ?></td>
				<td><?php echo $rows->bank_name; ?></td>
				<td><?php echo $rows->branch_name; ?></td>
				<td><?php echo number_format($rows->sum, 2); ?></td>
				<td>
					<?php 
						if($rows->cheque_status == "รออนุมัติ") {
							echo "<a href='javascript:approvedCheque(\"$rows->cheque_no\")' data-toggle='tooltip' title='อนุมัติเช็ค'><i class='icon-ok'></i></a>&nbsp;<a href='javascript:disapprovalCheque(\"$rows->cheque_no\")' data-toggle='tooltip' title='ยกเลิกเช็ค'><i class='icon-remove'></i></a>
							";
							
						} else {
							echo $rows->cheque_status;
						 } 
					?>
				</td>
	 			<td>
					<?php 
						if ($rows->cheque_status == "อนุมัติ") {
							if($rows->paid_status == "รอจ่าย") {
								echo "<a href='javascript:paidCheque(\"$rows->cheque_no\")' data-toggle='tooltip' title='จ่ายเช็ค'><i class='icon-ok'></i></a>";
								
							} else {
								echo $rows->paid_status;
							}
						} else {
							echo "รอสถานะเช็ค";
						}
						?>
				</td>
				
			</tr>
			<?php
					
				}
			?>

		</tbody>


	</table>
